<template>
	<view class="withdraw-container">
		<!-- 账户余额展示 -->
		<view class="balance-area">
			<view class="balance-title">可提现余额(元)</view>
			<view class="balance-amount">{{ enableNum.remainAmount }}</view>
		</view>

		<!-- 提现金额输入框 -->
		<view class="input-area">
			<view class="input-title">提现金额</view>
			<view class="input-wrapper">
				<text class="input-prefix">￥</text>
				<input class="amount-input" type="digit" v-model="withdrawAmount" placeholder="请输入提现金额"
					@blur="validateAmount" />
			</view>
			<view class="input-tips">
				<text class="tip-text">最低提现金额 {{ minWithdraw }}元</text>
				<text class="all-withdraw" @tap="withdrawAll">全部提现</text>
			</view>
		</view>

		<!-- 提现方式选择 -->
		<view class="withdraw-method">
			<view class="method-title">提现方式:</view>
			<view style="text-indent: 1em;"> 提现前需要完成实名认证，奖励提现到登录本小程序的微信账号</view>
		</view>

		<!-- 提现按钮 -->
		<view class="submit-area">
			<button class="withdraw-btn" :disabled="!canWithdraw" :class="{ disabled: !canWithdraw }"
				@tap="submitWithdraw">
				提现
			</button>
			<view class="withdraw-tip">预计1-3个工作日内到账，周末和法定节假日顺延</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				withdrawAmount: '', // 提现金额
				minWithdraw: 1, // 最低提现金额
				enableNum: {}
			}
		},
		computed: {
			// 是否可以提现
			canWithdraw() {
				const amount = parseFloat(this.withdrawAmount);
				return (
					amount >= this.minWithdraw &&
					amount <= this.enableNum.remainAmount
				);
			}
		},
		onShow() {
			this.getEnableNum();
		},
		methods: {
			// 验证提现金额
			validateAmount() {
				if (this.withdrawAmount === '') return;

				let amount = parseFloat(this.withdrawAmount);
				// 不能超过余额
				if (amount > this.enableNum.remainAmount) {
					this.withdrawAmount = this.enableNum.remainAmount;
				}
			},

			// 全部提现
			withdrawAll() {
				this.withdrawAmount = this.enableNum.remainAmount.toFixed(2);
			},

			getEnableNum() {
				//获取剩余次数
				this.$Request.postFormHost(this.$Api.getEnableNum).then(res => {
					if (res.status == 0) {
						this.enableNum = res.data;
					}
				})
			},


			// 提交提现
			submitWithdraw() {
				if (!this.canWithdraw) return;

				uni.showLoading({
					title: '提交中...'
				});
				this.$Request.postFormHost(this.$Api.userWithdraw, {
					amount: this.withdrawAmount
				}).then(res => {
					if (res.status === 0) {
						uni.showToast({
							title: '提现申请成功',
							icon: 'none'
						});
						// 重置表单
						this.withdrawAmount = '';
						// 刷新余额
						this.getEnableNum();
					} else if (res.status === -10) {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
						//跳转到实名认证页面
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/mine/userBind?type=1'
							})
						}, 2000);
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
					}
				});
			},

		}
	}
</script>

<style>
	.withdraw-container {
		padding: 30rpx;
		min-height: 90vh;
	}

	/* 账户余额样式 */
	.balance-area {
		background-color: #ffffff;
		border-radius: 12rpx;
		padding: 40rpx;
		margin-bottom: 30rpx;
		text-align: center;
	}

	.balance-title {
		font-size: 28rpx;
		color: #999999;
		margin-bottom: 20rpx;
	}

	.balance-amount {
		font-size: 60rpx;
		font-weight: bold;
		color: #333333;
	}

	/* 提现金额输入区域 */
	.input-area {
		background-color: #ffffff;
		border-radius: 12rpx;
		padding: 30rpx;
		margin-bottom: 30rpx;
	}

	.input-title {
		font-size: 30rpx;
		color: #333333;
		margin-bottom: 20rpx;
	}

	.input-wrapper {
		display: flex;
		align-items: center;
		border-bottom: 1px solid #eeeeee;
		padding-bottom: 20rpx;
		margin-bottom: 20rpx;
	}

	.input-prefix {
		font-size: 40rpx;
		color: #333333;
		margin-right: 10rpx;
	}

	.amount-input {
		flex: 1;
		font-size: 40rpx;
		color: #333333;
	}

	.input-tips {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.tip-text {
		font-size: 24rpx;
		color: #999999;
	}

	.all-withdraw {
		font-size: 24rpx;
		color: #007aff;
	}

	/* 提现方式选择 */
	.withdraw-method {
		background-color: #ffffff;
		border-radius: 12rpx;
		padding: 30rpx;
		margin-bottom: 30rpx;
		color: #505050;
	}

	.method-title {
		font-size: 30rpx;
		color: #333333;
		margin-bottom: 20rpx;
	}

	/* 提现按钮 */
	.submit-area {
		padding: 30rpx 0;
	}

	.withdraw-btn {
		width: 100%;
		height: 90rpx;
		background-color: #ff0000c0;
		color: #ffffff;
		border-radius: 45rpx;
		font-size: 32rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-bottom: 20rpx;
	}

	.withdraw-btn.disabled {
		background-color: #cccccc;
	}

	.withdraw-tip {
		text-align: center;
		font-size: 24rpx;
		color: #5e5e5e;
	}
</style>